<?php

use Drupal\wx_pay\Entity\WxPayTransaction;
use Drupal\wx_pay_refund\WxPayRefundController;

/**
 * Implements hook_rules_action_info().
 */
function wx_pay_refund_rules_action_info() {
  return [
    'wx_pay_refund_action_refund' => [
      'label' => t('由一个微信支付记录发起退款，返回一个退款记录'),
      'parameter' => [
        'wx_pay_transaction' => [
          'type' => 'wx_pay_transaction',
          'label' => t('微信支付记录'),
        ],
        'refund_desc' => [
          'type' => 'text',
          'label' => t('退款原因'),
        ],
        'refund_fee' => [
          'type' => 'decimal',
          'label' => t('需要退款的金额（单位:分）'),
          'description' => t('不填或大于支付记录金额都全额退款.'),
          'optional' => TRUE,
        ],
      ],
      'provides' => [
        'wx_pay_refund_record' => [
          'type' => 'wx_pay_refund_record',
          'label' => t('退款记录'),
        ],
      ],
      'group' => t('Wx Pay Refund'),
    ],
  ];
}


function wx_pay_refund_action_refund(WxPayTransaction $wxPayTransaction, $refund_desc, $refund_fee = 0) {
  $wxPayRefundController = new WxPayRefundController;
  $refund = $wxPayRefundController->refundTransaction('common', $wxPayTransaction, $refund_fee, $refund_desc);
  return ['wx_pay_refund_record' => $refund];
}
